package com.ming.multifunction.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ming.multifunction.common.ErrorCode;
import com.ming.multifunction.exception.BusinessException;
import com.ming.multifunction.mapper.PostMapper;
import com.ming.multifunction.model.domain.Post;
import com.ming.multifunction.model.domain.User;
import com.ming.multifunction.model.dto.PostQuery;
import com.ming.multifunction.model.vo.PostUserVO;
import com.ming.multifunction.model.vo.UserVO;
import com.ming.multifunction.service.PostService;
import com.ming.multifunction.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 123
 * @description 针对表【post(帖子)】的数据库操作Service实现
 * @createDate 2024-03-11 17:06:00
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post>
        implements PostService {

    @Resource
    private UserService userService;



    @Override
    public List<PostUserVO> listPost(PostQuery postQuery, boolean isAdmin) {

        if (postQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }


        LambdaQueryWrapper<Post> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (postQuery != null) {

            Long id = postQuery.getId();
            lambdaQueryWrapper.eq(id != null && id > 0, Post::getId, id);


            String title = postQuery.getTitle();
            lambdaQueryWrapper.like(StringUtils.isNotBlank(title), Post::getTitle, title);


            String description = postQuery.getDescription();
            lambdaQueryWrapper.eq(StringUtils.isNotBlank(description), Post::getDescription, description);


        }

        List<Post> postList = this.list(lambdaQueryWrapper);

        if (CollectionUtils.isEmpty(postList)) {
            return new ArrayList<>();
        }

        ArrayList<PostUserVO> postUserVOList = new ArrayList<>();

        //遍历每一个帖子，获取到用户的id，然后根据userId,把用户表的用户信息查出来
        for (Post post:postList){

            Long userId = post.getUserId();
            if (userId==null){
                continue;
            }
            if (post==null){
                continue;
            }

            PostUserVO postUserVO = new PostUserVO();
            //赋值帖子的内容给View Object
            BeanUtils.copyProperties(post,postUserVO);
            //根据userId,把用户表的用户信息查出来
            User user = userService.getById(userId);
            if (user!=null){
                UserVO userVO = new UserVO();
                //将user赋值给脱敏的UserVO
                BeanUtils.copyProperties(user,userVO);
                //设置贴子发布者的个人信息
                postUserVO.setCreateUser(userVO);
            }
            //将每条添加到整个数组列表
            postUserVOList.add(postUserVO);

        }
        return postUserVOList;
    }

    @Override
    public PostUserVO getPost(Long id) {

        Post post = this.getById(id);
        if(post==null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"该帖子不存在");
        }
        PostUserVO postUserVO=new PostUserVO();
        BeanUtils.copyProperties(post,postUserVO);
        Long userId = post.getUserId();
        User user = userService.getById(userId);
        if (user!=null){
            UserVO userVO = new UserVO();
            //将user赋值给脱敏的UserVO
            BeanUtils.copyProperties(user,userVO);
            //设置贴子发布者的个人信息
            postUserVO.setCreateUser(userVO);
        }

        return postUserVO;
    }

    @Override
    public long publishPost(Post post, User loginUser) {

        if (post==null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"帖子内容不能为空");
        }
        //  是否登录，未登录不允许发布
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }

        String title = post.getTitle();
        if (StringUtils.isBlank(title)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"帖子标题长不能为空");
        }
        if (title.length()>30){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"帖子标题长度不能超过30");
        }

        String description = post.getDescription();
        if (StringUtils.isBlank(description)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"描述内容长不能为空");
        }
        if ( description.length()>512){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"描述内容长度不能超过512");
        }

        Date createTime = post.getCreateTime();
        if (createTime==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"日期时间不能为空");
        }
        if (new Date().before(createTime)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"丢失时间大于当前时间");
        }

        String address = post.getAddress();
        if(StringUtils.isBlank(address)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"地点不能为空");
        }
        if ( address.length()>20){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"地点长度不能超过20");
        }

        String imageUrl = post.getImageUrl();
        if (StringUtils.isBlank(imageUrl)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"图片不能为空");
        }

        final long userId = loginUser.getId();

        post.setUserId(userId);

        boolean save = this.save(post);
        Long postId = post.getId();
        if (!save || postId==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"发布帖子失败");
        }


        return postId;
    }

    @Override
    public List<PostUserVO> searchPost(String searchText) {

        if (StringUtils.isBlank(searchText)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"搜索关键词不能为空");
        }

        //先把所有的Post查出来存在内存当中
        LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
        List<Post> postList = this.list(wrapper);
        if (CollectionUtils.isEmpty(postList)) {
            return new ArrayList<>();
        }

        ArrayList<PostUserVO> postUserVOList = new ArrayList<>();

        //遍历每一个帖子，获取到用户的id，然后根据userId,把用户表的用户信息查出来
        for (Post post:postList){

            String title = post.getTitle();
            String description = post.getDescription();
            //描述和标题都不包含搜索关键词则进行下一轮遍历
            if (!StringUtils.containsIgnoreCase(title,searchText)&&!StringUtils.containsIgnoreCase(description,searchText)){
                continue;
            }

            Long userId = post.getUserId();
            if (userId==null){
                continue;
            }

            PostUserVO postUserVO = new PostUserVO();
            //赋值帖子的内容给View Object
            BeanUtils.copyProperties(post,postUserVO);
            //根据userId,把用户表的用户信息查出来
            User user = userService.getById(userId);
            if (user!=null){
                UserVO userVO = new UserVO();
                //将user赋值给脱敏的UserVO
                BeanUtils.copyProperties(user,userVO);
                //设置贴子发布者的个人信息
                postUserVO.setCreateUser(userVO);
            }
            //将每条添加到整个数组列表
            postUserVOList.add(postUserVO);

        }


        return postUserVOList;
    }
}




